﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using RpgLibrary.ItemClasses;
using RpgLibrary;

namespace RpgEditor
{
    public partial class FormShieldDetails : FormDetailBase, IDetailForm
    {
        

        #region Field Region
        
        ShieldData shield;
        
        #endregion
        
        #region Property Region
        
        public ShieldData Shield
        {
            get { return shield; }
            set { shield = value; }
        }

        public override IRpgDataObject DataObject
        {
            get
            {
                return (IRpgDataObject)shield;
            }
            set
            {
                shield = (ShieldData)value;
            }
        }

        #endregion
        
        #region Constructor Region
        
        public FormShieldDetails()
        {
            InitializeComponent();

            SetDataObjectValueFunction = SetShieldValue;
            LoadDataObjectValueFunction = LoadShieldValue;

        }

        #endregion
        
        #region Event Handler Region

        void btnAddAllowed_Click(object sender, EventArgs e)
        {
            while (lbAvailableClasses.SelectedItems.Count > 0)
            {
                lbAllowedClasses.Items.Add(lbAvailableClasses.SelectedItems[0]);
                lbAvailableClasses.Items.Remove(lbAvailableClasses.SelectedItems[0]);
            }
        }

        void btnRemoveAllowed_Click(object sender, EventArgs e)
        {
            while (lbAllowedClasses.SelectedItems.Count > 0)
            {
                lbAvailableClasses.Items.Add(lbAllowedClasses.SelectedItems[0]);
                lbAllowedClasses.Items.Remove(lbAllowedClasses.SelectedItems[0]);
            }
        }

        void LoadShieldValue()
        {
            foreach (string s in FormMain.EntityDataManager.CharacterData.Keys)
                lbAvailableClasses.Items.Add(s);
            
            if (shield != null)
            {
                tbName.Text = shield.Name;
                tbType.Text = shield.Type;
                nudPrice.Value = shield.Price;
                nudWeight.Value = (decimal)shield.Weight;
                nudDefenseValue.Value = shield.DefenseValue;
                nudDefenseModifier.Value = shield.DefenseModifier;
                
                foreach (string className in shield.AllowableClasses)
                {
                    if (lbAvailableClasses.Items.Contains(className))
                        lbAvailableClasses.Items.Remove(className);
                    
                    lbAllowedClasses.Items.Add(className);
                }
            }
        }



        private bool SetShieldValue()
        {
            if (string.IsNullOrEmpty(tbName.Text))
            {
                MessageBox.Show("You must enter a name for the item.");
                return false;
            }

            List<string> allowedClasses = new List<string>();
            foreach (object o in lbAllowedClasses.Items)
                allowedClasses.Add(o.ToString());

            shield = new ShieldData();
            shield.Name = tbName.Text;
            shield.Type = tbType.Text;
            shield.Price = (int)nudPrice.Value;
            shield.Weight = (float)nudWeight.Value;
            shield.DefenseValue = (int)nudDefenseValue.Value;
            shield.DefenseModifier = (int)nudDefenseModifier.Value;
            shield.AllowableClasses = allowedClasses.ToArray();

            return true;
        }

       

        #endregion
    }
}